package com.metal.gear.common.util.list;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

/***
 * 集合比较工具
 */
public class ListCompareUtil {
//
//    public static <K> ListCompareResult<? extends ListCompareObject<K>> doCompare(List<ListCompareObject<K>> comparerList,
//                                                                                     List<ListCompareObject<K>> comparedList) {
//
//        ListCompareResult<ListCompareObject<K>> result = new ListCompareResult<>();
//        Map<K, ListCompareObject<K>> comparerMap = comparerList.stream().collect(Collectors.toMap(
//                ListCompareObject::getCompareAttributes,
//                Function.identity()));
//        Map<K, ListCompareObject<K>> comparedMap = comparedList.stream().collect(Collectors.toMap(
//                ListCompareObject::getCompareAttributes,
//                Function.identity()));
//
//        List<ListCompareObject<K>> addList = comparerList.stream()
//                .filter((comparer) -> Objects.isNull(comparedMap.get(comparer.getCompareAttributes())))
//                .collect(Collectors.toList());
//
//        List<ListCompareObject<K>> lostList = comparedList.stream()
//                .filter((compared) -> Objects.isNull(comparerMap.get(compared.getCompareAttributes())))
//                .collect(Collectors.toList());
//        result.getNewList().addAll(addList);
//        result.getListList().addAll(lostList);
//        return result;
//
//    }


    public static <K, T extends ListCompareObject<K>> ListCompareResult<T> doCompare(List<T> comparerList,
                                                                                                                                                         List<T> comparedList) {

        ListCompareResult<T> result = new ListCompareResult<>();
        Map<K, T> comparerMap = comparerList.stream().collect(Collectors.toMap(
                ListCompareObject::getCompareAttributes,
                Function.identity()));
        Map<K, T> comparedMap = comparedList.stream().collect(Collectors.toMap(
                ListCompareObject::getCompareAttributes,
                Function.identity()));

        List<T> addList = comparerList.stream()
                .filter((comparer) -> Objects.isNull(comparedMap.get(comparer.getCompareAttributes())))
                .collect(Collectors.toList());

        List<T> lostList = comparedList.stream()
                .filter((compared) -> Objects.isNull(comparerMap.get(compared.getCompareAttributes())))
                .collect(Collectors.toList());

        result.setLostList(addList);
        result.setLostList(lostList);
        return result;

    }

    public static ListCompareResult<String> doCompareString(List<String> comparerList,
                                                                                              List<String> comparedList) {

        ListCompareResult<String> result = new ListCompareResult<>();
        Map<String, Integer> comparerMap
                = comparerList.stream().collect(Collectors.toMap(Function.identity(), (s) -> 1));

        Map<String, Integer> comparedMap
                = comparedList.stream().collect(Collectors.toMap(Function.identity(), (s) -> 1));


        List<String> addList = comparerList.stream()
                .filter((comparer) -> Objects.isNull(comparedMap.get(comparer)))
                .collect(Collectors.toList());

        List<String> lostList = comparedList.stream()
                .filter((compared) -> Objects.isNull(comparerMap.get(compared)))
                .collect(Collectors.toList());
        result.setNewList(addList);
        result.setLostList(lostList);
        return result;

    }


}
